#ifndef __CC_PORTABLE__
#define __CC_PORTABLE__

#define cc_config_use_bt 1

#include "cc_config.h"
#include <Arduino.h>
#include "assert.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#define CC_ASSERT(cond) \
  do                    \
  {                     \
    if (!(cond))        \
    {                   \
      assert(cond);     \
    }                   \
  } while (0)

#define cc_inline static inline
#define CC_NULL NULL
#define CC_USART1_BAUD 9600

// 阻塞延时 rtos可以不用这个
#define cc_sleep(ms) delay(ms);
#define cc_reboot() ESP.restart()

#define CC_TIME_WAIT_FOREVER portMAX_DELAY
#define cc_true (pdTRUE)
#define cc_false (pdFALSE)

// 互斥锁
typedef struct
{
  StaticSemaphore_t mut;
} cc_mutex, *cc_mutex_t;
// 信号量
typedef struct
{
  StaticSemaphore_t sem;
} cc_sem, *cc_sem_t;
// 条件变量
typedef struct
{
  cc_sem sem;
  uint32_t len;
} cc_cond, *cc_cond_t;
// 消息队列
typedef struct
{
  StaticQueue_t entity;
  QueueHandle_t que;
  int capacity;
  int len;
} cc_que, *cc_que_t;

// 定时器
typedef void (*cc_timer_cb)(void *data);
typedef struct
{
  StaticTimer_t entity;
  TimerHandle_t timer;
} cc_timer, *cc_timer_t;

// 链表
typedef List_t cc_list;
typedef ListItem_t cc_list_node;

// 内存
#define cc_free(mem) vPortFree(mem)
#define cc_malloc(mem) pvPortMalloc(mem)
// 线程
#define cc_delay(ms) vTaskDelay(ms);
typedef TaskHandle_t cc_tid;


#define cc_tid_fun TaskFunction_t

#define CC_ERROR -1
#define CC_EOK 0
#define CC_EPERM 1         /* Operation not permitted */
#define CC_ENOENT 2        /* No such file or directory */
#define CC_ESRCH 3         /* No such process */
#define CC_EINTR 4         /* Interrupted system call */
#define CC_EIO 5           /* I/O error */
#define CC_ENXIO 6         /* No such device or address */
#define CC_E2BIG 7         /* Argument list too long */
#define CC_ENOEXEC 8       /* Exec format error */
#define CC_EBADF 9         /* Bad file number */
#define CC_ECHILD 10       /* No child processes */
#define CC_EAGAIN 11       /* Try again */
#define CC_ENOMEM 12       /* Out of memory */
#define CC_EACCES 13       /* Permission denied */
#define CC_EFAULT 14       /* Bad address */
#define CC_ENOTBLK 15      /* Block device required */
#define CC_EBUSY 16        /* Device or resource busy */
#define CC_EEXIST 17       /* File exists */
#define CC_EXDEV 18        /* Cross-device link */
#define CC_ENODEV 19       /* No such device */
#define CC_ENOTDIR 20      /* Not a directory */
#define CC_EISDIR 21       /* Is a directory */
#define CC_EINVAL 22       /* Invalid argument */
#define CC_ENFILE 23       /* File table overflow */
#define CC_EMFILE 24       /* Too many open files */
#define CC_ENOTTY 25       /* Not a typewriter */
#define CC_ETXTBSY 26      /* Text file busy */
#define CC_EFBIG 27        /* File too large */
#define CC_ENOSPC 28       /* No space left on device */
#define CC_ESPIPE 29       /* Illegal seek */
#define CC_EROFS 30        /* Read-only file system */
#define CC_EMLINK 31       /* Too many links */
#define CC_EPIPE 32        /* Broken pipe */
#define CC_EDOM 33         /* Math argument out of domain of func */
#define CC_ERANGE 34       /* Math result not representable */
#define CC_EDEADLK 35      /* Resource deadlock would occur */
#define CC_ENAMETOOLONG 36 /* File name too long */
#define CC_ENOLCK 37       /* No record locks available */
#define CC_ENOSYS 38       /* Function not implemented */
#define CC_ENOTEMPTY 39    /* Directory not empty */
#define CC_ELOOP 40        /* Too many symbolic links encountered */
#define CC_EWOULDBLOCK 41  /* Operation would block */
#define CC_ENOMSG 42       /* No message of desired type */
#define CC_EIDRM 43        /* Identifier removed */
#define CC_ECHRNG 44       /* Channel number out of range */
#define CC_EL2NSYNC 45     /* Level 2 not synchronized */
#define CC_EL3HLT 46       /* Level 3 halted */
#define CC_EL3RST 47       /* Level 3 reset */
#define CC_ELNRNG 48       /* Link number out of range */
#define CC_EUNATCH 49      /* Protocol driver not attached */
#define CC_ENOCSI 50       /* No CSI structure available */
#define CC_EL2HLT 51       /* Level 2 halted */
#define CC_EBADE 52        /* Invalid exchange */
#define CC_EBADR 53        /* Invalid request descriptor */
#define CC_EXFULL 54       /* Exchange full */
#define CC_ENOANO 55       /* No anode */
#define CC_EBADRQC 56      /* Invalid request code */
#define CC_EBADSLT 57      /* Invalid slot */
#define CC_EDEADLOCK 58
#define CC_EBFONT 59           /* Bad font file format */
#define CC_ENOSTR 60           /* Device not a stream */
#define CC_ENODATA 61          /* No data available */
#define CC_ETIME 62            /* Timer expired */
#define CC_ENOSR 63            /* Out of streams resources */
#define CC_ENONET 64           /* Machine is not on the network */
#define CC_ENOPKG 65           /* Package not installed */
#define CC_EREMOTE 66          /* Object is remote */
#define CC_ENOLINK 67          /* Link has been severed */
#define CC_EADV 68             /* Advertise error */
#define CC_ESRMNT 69           /* Srmount error */
#define CC_ECOMM 70            /* Communication error on send */
#define CC_EPROTO 71           /* Protocol error */
#define CC_EMULTIHOP 72        /* Multihop attempted */
#define CC_EDOTDOT 73          /* RFS specific error */
#define CC_EBADMSG 74          /* Not a data message */
#define CC_EOVERFLOW 75        /* Value too large for defined data type */
#define CC_ENOTUNIQ 76         /* Name not unique on network */
#define CC_EBADFD 77           /* File descriptor in bad state */
#define CC_EREMCHG 78          /* Remote address changed */
#define CC_ELIBACC 79          /* Can not access a needed shared library */
#define CC_ELIBBAD 80          /* Accessing a corrupted shared library */
#define CC_ELIBSCN 81          /* .lib section in a.out corrupted */
#define CC_ELIBMAX 82          /* Attempting to link in too many shared libraries */
#define CC_ELIBEXEC 83         /* Cannot exec a shared library directly */
#define CC_EILSEQ 84           /* Illegal byte sequence */
#define CC_ERESTART 85         /* Interrupted system call should be restarted */
#define CC_ESTRPIPE 86         /* Streams pipe error */
#define CC_EUSERS 87           /* Too many users */
#define CC_ENOTSOCK 88         /* Socket operation on non-socket */
#define CC_EDESTADDRREQ 89     /* Destination address required */
#define CC_EMSGSIZE 90         /* Message too long */
#define CC_EPROTOTYPE 91       /* Protocol wrong type for socket */
#define CC_ENOPROTOOPT 92      /* Protocol not available */
#define CC_EPROTONOSUPPORT 93  /* Protocol not supported */
#define CC_ESOCKTNOSUPPORT 94  /* Socket type not supported */
#define CC_EOPNOTSUPP 95       /* Operation not supported on transport endpoint */
#define CC_EPFNOSUPPORT 96     /* Protocol family not supported */
#define CC_EAFNOSUPPORT 97     /* Address family not supported by protocol */
#define CC_EADDRINUSE 98       /* Address already in use */
#define CC_EADDRNOTAVAIL 99    /* Cannot assign requested address */
#define CC_ENETDOWN 100        /* Network is down */
#define CC_ENETUNREACH 101     /* Network is unreachable */
#define CC_ENETRESET 102       /* Network dropped connection because of reset */
#define CC_ECONNABORTED 103    /* Software caused connection abort */
#define CC_ECONNRESET 104      /* Connection reset by peer */
#define CC_ENOBUFS 105         /* No buffer space available */
#define CC_EISCONN 106         /* Transport endpoint is already connected */
#define CC_ENOTCONN 107        /* Transport endpoint is not connected */
#define CC_ESHUTDOWN 108       /* Cannot send after transport endpoint shutdown */
#define CC_ETOOMANYREFS 109    /* Too many references: cannot splice */
#define CC_ETIMEDOUT 110       /* Connection timed out */
#define CC_ECONNREFUSED 111    /* Connection refused */
#define CC_EHOSTDOWN 112       /* Host is down */
#define CC_EHOSTUNREACH 113    /* No route to host */
#define CC_EALREADY 114        /* Operation already in progress */
#define CC_EINPROGRESS 115     /* Operation now in progress */
#define CC_ESTALE 116          /* Stale NFS file handle */
#define CC_EUCLEAN 117         /* Structure needs cleaning */
#define CC_ENOTNAM 118         /* Not a XENIX named type file */
#define CC_ENAVAIL 119         /* No XENIX semaphores available */
#define CC_EISNAM 120          /* Is a named type file */
#define CC_EREMOTEIO 121       /* Remote I/O error */
#define CC_EDQUOT 122          /* Quota exceeded */
#define CC_ENOMEDIUM 123       /* Nomedium found */
#define CC_EMEDIUMTYEP 124     /*Wrongmedium found */
#define CC_ECANCELED 125       /* Operation Canceled */
#define CC_ENOKEY 126          /* Required key not available */
#define CC_EKEYEXPIRED 127     /* Key has expired */
#define CC_EKEYREVOKED 128     /* Key has been revoked */
#define CC_EKEYREJECTED 129    /* Key was rejected by service */
#define CC_EOWNERDEAD 130      /* Owner died */
#define CC_ENOTRECOVERABLE 131 /* State not recoverable */
#define CC_ERFKILL 132         /* Operation not possible due to RF-kill */
#define CC_EHWPOISON 133       /* Memory page has hardware error */

#endif
